﻿namespace Xms.Abp.Components;

public class NNumberColumn<TNumber> : Column<TNumber?>
    where TNumber : struct, IFormattable
{
    public NNumberColumn()
    {
        Style = "text-align:right;";
        HeaderStyle = "text-align:right;";
        Width = "120";
        if (typeof(TNumber) == typeof(decimal))
        {
            Format = "N4";
        }
        else if (typeof(TNumber) == typeof(float) || typeof(TNumber) == typeof(double))
        {
            Format = "N6";
        }
        ChildContent = builder =>
        {
            if (Field.HasValue)
            {
                if (!Format.IsNullOrWhiteSpace())
                {
                    builder.AddContent(0, Field.Value.ToString(Format, null));
                }
                else
                {
                    builder.AddContent(0, Field.Value.ToString());
                }
            }
            else
            {
                builder.AddContent(0, "-");
            }
        };
    }
}
